package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.dao.LableDao;
import cn.yunyichina.provider.bone.dao.LableDoctorDao;
import cn.yunyichina.provider.bone.entity.Lable;
import cn.yunyichina.provider.bone.entity.LableDoctor;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhuxp on 2018/1/16.
 * 标签管理
 * @param
 * @Descrition
 */
@Service
@Log4j
public class LableServiceImpl {

    @Autowired
    private LableDao lableDao;
    @Autowired
    private LableDoctorDao lableDoctorDao;

    /**
     * 获取所有标签
     * yyt.bone.lable.list
     * @return
     */
    public Response listLable(){
        Response response = new Response();
        try {
            Lable lable = new Lable();
            lable.setAttributeType(1);
            List<Lable> lables = lableDao.select(lable);
            response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE, JsonUtils.toJsonString(lables));
        }catch (Exception e){
            log.error("查询标签异常!"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,Response.FAILURE_MESSAGE);
        }
        return response;
    }

    /**
     *  新增标签
     *  yyt.bone.lable.insert
     * @param data
     * @return
     */
    public  Response insertLable(String data){
        log.info("新增标签入参："+data);
        Response response = new Response();
        try {
            Lable lable = JsonUtils.parseObject(data,Lable.class);
            if(StringUtils.isBlank(lable.getAttributeName())){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签名不能为空!");
                return response;
            }
            Lable entity = new Lable();
            entity.setAttributeName(lable.getAttributeName());
            entity.setAttributeType(1);
            List<Lable> entitys = lableDao.select(entity);
            if(!entitys.isEmpty()){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签名已存在!");
                return response;
            }
            lable.setId(PKGenerator.generateId());
            lable.setAttributeType(1);
            lable.setCreateTime(DateUtils.getDateTimeStr());
            lable.setUpdateTime(DateUtils.getDateTimeStr());
            lableDao.insert(lable);
            response.setResultCodeAndMessage(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE);
        }catch (Exception e){
            log.error("新增标签异常！"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,Response.FAILURE_MESSAGE);
        }
        return  response;
    }

    /**
     * yyt.bone.lable.update
     * @param data
     * @return
     */
    public Response updateLable(String data){
        log.info("更新标签入参："+data);
        Response response = new Response();
        try {
            Lable lable = JsonUtils.parseObject(data,Lable.class);
            if(StringUtils.isBlank(lable.getAttributeName())||StringUtils.isBlank(lable.getId())){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签名或id不能为空!");
                return response;
            }
            Lable entity = new Lable();
            entity.setAttributeName(lable.getAttributeName());
            List<Lable> entitys = lableDao.select(entity);
            if(!entitys.isEmpty()){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签名已存在!");
                return response;
            }
            lable.setUpdateTime(DateUtils.getDateTimeStr());
            lableDao.update(lable);
            response.setResultCodeAndMessage(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE);
        }catch (Exception e){
            log.error("更新标签异常！"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,Response.FAILURE_MESSAGE);
        }
        return  response;
    }


    /**
     * yyt.bone.lable.delete
     * @param data
     * @return
     */
    public Response deleteLable(String data){
        log.info("删除标签入参："+data);
        Response response = new Response();
        try {
            Lable lable = JsonUtils.parseObject(data,Lable.class);
            if(StringUtils.isBlank(lable.getId())){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签id不能为空!");
                return response;
            }
            Lable result =lableDao.selectById(lable.getId());
            if (result== null){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"标签id有误!");
                return response;
            }
            lableDao.delete(result);
            //删除医生标签关系表记录
            LableDoctor lableDoctor = new LableDoctor();
            lableDoctor.setLableId(result.getId());
            List<LableDoctor> lableDoctorList = lableDoctorDao.select(lableDoctor);
            if(!lableDoctorList.isEmpty()){
                for(LableDoctor ld:lableDoctorList){
                    lableDoctorDao.delete(ld);
                }
            }
            response.setResultCodeAndMessage(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE);
        }catch (Exception e){
            log.error("删除标签异常！"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,Response.FAILURE_MESSAGE);
        }
        return  response;
    }
}
